package com.zx.deploy.controller;

import java.io.IOException;
import java.net.URISyntaxException;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.zx.deploy.common.utils.FileUtil;
import com.zx.deploy.config.DeployConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zx.deploy.common.exception.BusinessException;
import com.zx.deploy.common.utils.JsonUtil;
import com.zx.deploy.common.utils.ReturnModel;
import com.zx.deploy.model.SpeedProgress;
import com.zx.deploy.service.DeployService;
import com.zx.deploy.service.UploadService;

/**
 * 部署
 */
@RequestMapping("/dep")
@Controller
public class DeployController {

    @Resource
    private UploadService uploadService;

    @Resource
    private DeployService deployService;

    @Resource
    private DeployConfig deployConfig;

    /**
     * 上传升级文件
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public Object upload(MultipartFile file){
        try {
            uploadService.upload(file);
        }catch (BusinessException e){
            e.printStackTrace();
            return ReturnModel.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnModel.error("上传过程中出现异常");
        } 
        return ReturnModel.success();
    }

    /**
     * 跳转到升级进度页面
     * @return
     */
    @RequestMapping("/deployProgressIndex")
    public String deployProgressIndex(){
        return "deploy_progress";
    }

    /**
     * 跳转到升级进度页面
     * @return
     */
    @RequestMapping("/versionsIndex")
    public String versionsIndex(){
        return "versions";
    }

    @ResponseBody
    @RequestMapping(value = "/queryProgress", produces = "text/event-stream;charset=UTF-8")
    public Object queryProgress(HttpSession session) throws IOException, InterruptedException {
        SpeedProgress speed = (SpeedProgress)session.getAttribute("speed");
        String retry = "retry:1000\n";
        if(speed == null){
            return retry + "data:" + JsonUtil.jsonEncode(ReturnModel.success("成功",new SpeedProgress())) + "\n\n";
        }
        return retry + "data:" + JsonUtil.jsonEncode(ReturnModel.success("成功",speed)) + "\n\n";
    }

    /**
     * 升级
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public Object update(HttpSession session){
        try {
            session.setAttribute("speed",deployService.update());
            return ReturnModel.success();
        } catch (Exception e) {
            return ReturnModel.error("升级失败，请联系管理员排查");
        }
    }

    /**
     * 版本列表
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Object list(){
        try {
            return ReturnModel.success("成功", FileUtil.listFiles(deployConfig.backups_war_path,""));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnModel.error("查询备份列表失败");
        }
    }

}
